<template>
  <div>
    <el-input
      v-model="filterText"
      placeholder="输入查询条件"
      style="margin-bottom:30px;" />

    <el-tree
      ref="subjectTree"
      :data="items"
      :props="defaultProps"
      :filter-node-method="filterNode"
      style="margin-top:10px;" />
  </div>
</template>
<script>
import subjectApi from '@/api/subject'
export default {
  data() {
    return {
      filterText: '', // 过滤文本
      items: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.subjectTree.filter(val) // 调用tree标签的过滤方法
    }
  },
  created() {
    this.fetchNodeList()
  },
  methods: {
    // 获取远程数据
    fetchNodeList() {
      subjectApi.getNestedTreeList().then(response => {
        this.items = response.data.items
      })
    },
    // 过滤节点
    filterNode(value, data) { // value代表改变后的filterText的值，data代表每个节点的 json对象
      if (!value) return true
      return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1// 忽略大小写
    }
  }
}
</script>
